package com.bw.goods.mvvm.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ConvertUtils;
import com.bw.goods.mvvm.model.entity.HomeBannerEntity;
import com.bw.mvvm_common.network.load.LoadImage;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

public class HomeTopAdapter extends PagerAdapter {

    private List<HomeBannerEntity> topEntities;

    public HomeTopAdapter(List<HomeBannerEntity> topEntities){
        this.topEntities = topEntities;
    }

    @Override
    public int getCount() {
        return topEntities.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull @NotNull View view, @NonNull @NotNull Object object) {
        return view == object;
    }

    @NonNull
    @NotNull
    @Override
    public Object instantiateItem(@NonNull @NotNull ViewGroup container, int position) {
        ImageView img = new ImageView(container.getContext());
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        RelativeLayout relativeLayout = new RelativeLayout(container.getContext());
        relativeLayout.setLayoutParams(lp);
        RelativeLayout.LayoutParams relativeLp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        relativeLp.leftMargin = ConvertUtils.dp2px(10);
        relativeLp.rightMargin = ConvertUtils.dp2px(10);
        img.setLayoutParams(relativeLp);
        relativeLayout.addView(img);
        container.addView(relativeLayout);
        LoadImage.loadMatchImg(topEntities.get(position).imgUrl,img);
        return relativeLayout;
    }

    @Override
    public void destroyItem(@NonNull @NotNull ViewGroup container, int position, @NonNull @NotNull Object object) {
         View view = (View) object;
         container.removeView(view);
    }
}
